package com.henry.chatkit.input

import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import android.util.AttributeSet
import android.widget.*
import androidx.fragment.app.FragmentActivity
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.PermissionUtils
import com.henry.chatkit.R

abstract class InputLayoutUI : LinearLayout {

    protected val CAPTURE = 1
    protected val AUDIO_RECORD = 2
    protected val VIDEO_RECORD = 3
    protected val SEND_PHOTO = 4
    protected val SEND_FILE = 5

    protected var mAudioDisable = false
    protected var mFaceDisable = false
    protected var mMoreDisable = false

    protected var mActivity: FragmentActivity? = null
    private var mPermissionDialog: AlertDialog? = null

    val audioButton: ImageView by lazy { findViewById(R.id.audioButton) }
    val input: EditText by lazy { findViewById(R.id.input) }
    val voiceButton: TextView by lazy { findViewById(R.id.voiceButton) }
    val faceButton: ImageView by lazy { findViewById(R.id.faceButton) }
    val moreButton: ImageView by lazy { findViewById(R.id.moreButton) }
    val sendButton: TextView by lazy { findViewById(R.id.sendButton) }
    val moreGroups: RelativeLayout by lazy { findViewById(R.id.moreGroups) }

    constructor(context: Context?) : super(context) {
        initViews()
    }
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        initViews()
    }
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initViews()
    }

    private fun initViews() {
        mActivity = context as FragmentActivity
        inflate(context, R.layout.chat_input_layout, this)

        initView()
    }

    protected abstract fun initView()

    fun disableAudio(disable: Boolean) {
        mAudioDisable = disable
        if (disable) {
            audioButton.visibility = GONE
        } else {
            audioButton.visibility = VISIBLE
        }
    }

    fun disableFace(disable: Boolean) {
        mFaceDisable = disable
        if (disable) {
            faceButton.visibility = GONE
        } else {
            faceButton.visibility = VISIBLE
        }
    }

    fun disableMore(disable: Boolean) {
        mMoreDisable = disable
        if (disable) {
            moreButton.visibility = GONE
        } else {
            moreButton.visibility = VISIBLE
        }
    }

    protected open fun showMoreButton(visibility: Int) {
        if (mMoreDisable) {
            return
        }
        moreButton.visibility = visibility
    }

    protected open fun showSendButton(visibility: Int) {
        if (mMoreDisable) {
            sendButton.visibility = VISIBLE
        } else {
            sendButton.visibility = visibility
        }
    }

    protected open fun showFaceButton(visibility: Int) {
        if (mFaceDisable) {
            return
        }
        faceButton.visibility = visibility
    }

    open fun showPermissionDialog() {
        if (mPermissionDialog == null) {
            mPermissionDialog = AlertDialog.Builder(mActivity)
                .setMessage("使用该功能，需要开启权限，鉴于您禁用相关权限，请手动设置开启权限")
                .setPositiveButton("设置") { dialog, which ->
                    cancelPermissionDialog()
                    val packageURI = Uri.parse("package:" + mActivity!!.packageName)
                    val intent =
                        Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI)
                    mActivity!!.startActivity(intent)
                }
                .setNegativeButton("取消") { dialog, which -> //关闭页面或者做其他操作
                    cancelPermissionDialog()
                }
                .create()
        }
        mPermissionDialog?.show()
    }

    open fun cancelPermissionDialog() {
        mPermissionDialog?.cancel()
    }

    open fun checkPermission(type: Int, callback: (Boolean) -> Unit) {
        when (type) {
            AUDIO_RECORD -> {
                val grant = PermissionUtils.isGranted(PermissionConstants.MICROPHONE, PermissionConstants.STORAGE)
                if (!grant) {
                    showPermissionDialog()
                }
                callback.invoke(grant)

//                PermissionUtils.permission(PermissionConstants.MICROPHONE, PermissionConstants.STORAGE).callback { isAllGranted, granted, deniedForever, denied ->
//                    if (!isAllGranted) {
//                        showPermissionDialog()
//                    }
//                    callback.invoke(isAllGranted)
//                }
            }
            VIDEO_RECORD -> {
                PermissionUtils.permission(PermissionConstants.CAMERA, PermissionConstants.MICROPHONE, PermissionConstants.STORAGE).callback { isAllGranted, granted, deniedForever, denied ->
                    if (!isAllGranted) {
                        showPermissionDialog()
                    }
                    callback.invoke(isAllGranted)
                }
            }
            CAPTURE -> {
                PermissionUtils.permission(PermissionConstants.CAMERA, PermissionConstants.STORAGE).callback { isAllGranted, granted, deniedForever, denied ->
                    if (!isAllGranted) {
                        showPermissionDialog()
                    }
                    callback.invoke(isAllGranted)
                }
            }

        }


    }

}